import {observable, action} from 'mobx'
import { WeaForm } from 'comsMobx'
import * as API_LIST from '../../apis/fnaInvoiceIdentify'
import * as FnaMessage from '../../public/FnaMessage'
import {ConstantStatus} from '../../public/FnaConstant'

/**
 * Store
 */
export class RecognitionStore {

	constructor(){
    }

    @observable loading = false;

    setLoding = (value) => {
        this.loading = value;
    }

    /** 路由 **/
    router = {};
    
    //设置路由
    setRouter = (value) => {
        this.router = value;
    }

    /****************** 识别 *******************/
    //将识别成功的 发票存起来
    recognitionInvocieIds = [];

    @observable recognitionForm = new WeaForm();
    @observable recognitionCondition = [];
    //文件上传后的ids值
    @observable excelFileNo = [];

    @observable guid = '';

    //保存上传后的ids
    setExcelFileNo = (value) => {
        this.excelFileNo = value;
    }
    /*****************************/
    
    @observable hasRight;//权限的标记 4无权限，跳转

    //识别弹出框显隐
    @observable recognitionModalVisible = false;

    //识别弹出框显隐
    setRecognitionModal = (value) => {
        this.recognitionModalVisible = value;
    }

    uploadEndMsg = [];

    //识别
    doTrainRecognition = (value = {},excelFileNo,callBack) => {
        API_LIST.doRecognition(value).then( data => {
            let {status,uploadMsg,ids} = data;
            if(status == ConstantStatus.FNA_RESULT_SUCCESS || status == ConstantStatus.FNA_RESULT_FAILED){
                if(status == ConstantStatus.FNA_RESULT_SUCCESS){
                    ids && this.recognitionInvocieIds.push();
                    if(!!uploadMsg){
                        this.uploadEndMsg.push(uploadMsg);
                    }
                } else if(status == ConstantStatus.FNA_RESULT_FAILED){
                    uploadMsg && FnaMessage.fnaWarningTip(uploadMsg);
                }
                
                if(excelFileNo.length == 0){
                    let tip = '';
                    if(this.uploadEndMsg.length > 0){
                        this.uploadEndMsg.map((item,index)=>{
                            tip += '<div>'+(index+1)+':'+item+'</div>'
                        });
                        
                    }
                    tip && FnaMessage.fnaWarning(tip);

                    this.uploadEndMsg = [];
                    this.loading = false;
                    callBack && callBack();
                    return;
                }

                let fileid = excelFileNo.shift();
                let formData = {
                    fptpId:fileid,
                    op:'recognition'
                };

                this.doTrainRecognition(formData,excelFileNo,callBack);

               
            }
        });
    }
}